/**
 * 日期格式化函数
 * @param date {string} 传入的日期
 * @param format {string} 需要格式化后的类型
 * @example formatDate('2021-01-01', 'yyyy/mm/dd')
 */

export const formatDate = (_date, format = 'yyyy/mm/dd') => {
	const date = new Date(_date * 1000);
	switch (format) {
		case 'yyyy-mm-dd':
			return `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(
				-2
			)}-${('0' + date.getDate()).slice(-2)}`;
		case 'yyyy/mm/dd':
			return `${date.getFullYear()}/${('0' + (date.getMonth() + 1)).slice(
				-2
			)}/${('0' + date.getDate()).slice(-2)}`;
		case 'yyyy':
			return date.getFullYear();
		case 'yy':
			return ('' + date.getFullYear).slice(-2);
		case 'M':
			return date.getMonth() + 1;
		case 'MM':
			return ('0' + (date.getMonth() + 1)).slice(-2);
		case 'd':
			return date.getDate();
		case 'dd':
			return ('0' + date.getDate()).slice(-2);
		case 'H':
			return date.getHours();
		case 'HH':
			return ('0' + date.getHours()).slice(-2);
		case 'h':
			return date.getHours() % 12;
		case 'hh':
			return ('0' + date.getHours()).slice(-2);
		case 'm':
			return date.getMinutes();
		case 'mm':
			return ('0' + date.getMinutes()).slice(-2);
		case 's':
			return date.getSeconds();
		case 'ss':
			return ('0' + date.getSeconds()).slice(-2);
		case 'w':
			return ['日', '一', '二', '三', '四', '五', '六'][date.getDay()];
		default:
			return;
	}
};

export const format = function (data, format) {
	const date = new Date(data);
	switch (format) {
		case 'MM-dd':
			return `${('0' + (date.getMonth() + 1)).slice(-2)}-${(
				'0' + date.getDate()
			).slice(-2)}`;
		case 'w':
			return ['日', '一', '二', '三', '四', '五', '六'][date.getDay()];
		default:
			return;
	}
};
